<?php

namespace app\official\controller;

//引入微信扩展类
use mikkle\tp_wechat\Wechat;
use mikkle\tp_wechat\WechatApi;
use mikkle\tp_wechat\src\User;
use mikkle\tp_wechat\src\Media;
use mikkle\tp_wechat\base\WechatBase;
use think\Model;
use think\Db;
use mikkle\tp_wechat\support\Curl;

// 单独引入视图层
use \think\View;

class Index extends WechatApi
{
    //定义微信公众号的配置参数
    protected $options = [
        'token' => 'YXC',
        'appid' => 'wxa927c4be329d3b57',  //开发者id
        'appsecret' => '2d29afb5efb3f02ebf32ce21880ad2f1', //开发者密码
        'encodingaeskey' => '0qtH6Fl1cxQHhKatgyb8lokTsOYNTS4NeKTXZUsZi4c',
    ];

    //网站第一次对接 true  第二次以后要改为false  
    protected $valid = false;

    //是否开启钩子
    protected $isHook = false;

    public function __construct()
    {
        //继承父类
        parent::__construct();

        //MVC 视图层
        $this->view = new View();

        //获取请求参数
        $this->request = request();
    }

    //服务器对接方法： 接收微信服务器给过来的请求
    //对接方法，既可以接收请求，发送数据回去

    public function auth()
    {
        //调用父类的index方法：接收微信请求并解析
        $this->index();
        //首次关注
        //returnEventSubscribe 封装了一条准备发送的数据结构
        // $subscribe = $this->returnEventSubscribe();

        // //回复回去(微信服务器)
        // $this->weObj->text($subscribe)->reply();
        // //直接调用回复信息
        // $str = $this->returnMessageText();
        // $this->weObj->text($str)->reply();

    }
    
    public function send()
    {
        // 获取粉丝列表的openid
        // https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
        $options = $this->options;
        $WechatBase = new WechatBase($options);
        $access_token = $WechatBase->getToken();
       
        $curl_url = "https://api.weixin.qq.com/cgi-bin/user/get?" . "access_token={$access_token}";
        $result = Curl::curlGet($curl_url);
        $param = json_decode($result, true);


        $this->ReleaseModel = model('ReleaseRelease');
        $this->Pallet = model('Pallet');
        // 查询数据
        $data = $this->ReleaseModel->where(["type" => 2])->order('releaserelease_id desc')->find();

        $releaseid = $data['releaserelease_id'];

        $onedata = $this->Pallet->where(['releaserelease_id' => $releaseid])->find();

        $shijian = $onedata['loading_date'];  //时间
        $tiji = $onedata['quantity'];    //箱型
        $port = $onedata['port'];  //起点和目的地

        $arrar = explode('-', $port);

        if (count($arrar) < 2) {
            $qi = $arrar[0];
            $su = "";
        } else if (count($arrar) == 2) {
            $qi = $arrar[0];
            $su = $arrar[1];
        } else {
            $qi = "上海";
            $su = "悉尼";
        }
        // var_dump($qi);
        // var_dump($su);
        // exit;

        $templateid = 'AXf5pSeFcUhaVv3iD5lxH-Y8Yh36rfe15pgJHsnnEG0';
        // $templateid = 'uYGH1QhOvwZh62Xol3tCcEOb9TmffPSjQKsIeWpWVA4';

        // // 接收人的openid、
        // $openid = $this->openid;

        // $openid = "oIR-45l3DJd9257u190TBjZ0zRA0";
        $openid= $param['data']['openid'];

        foreach($openid as $item){
            $data = [
                'touser' => $item,
                'template_id' => $templateid,
                "topcolor" => "#FF0000", //内容颜色
                // "url" => "http://weixin.qq.com/download",
                "miniprogram" => [
                    "appid" => "wx83e471acdb25657b",  //小程序appid
                    "pagepath" => "pages/square/square"  //所需跳转到小程序的具体页面路径，支持带参数,（示例index?foo=bar），要求该小程序已发布，暂不支持小游戏
                ],
                "data" => [
                    'first' => [
                        'value' => "尊敬的易现舱用户,为您推荐了热门现舱特价,快来看看吧！",
                        'color' => '#173177'
                    ],
                    'keyword1' => [
                        'value' => "舱位通知",
                        'color' => '#173177'
                    ],
                    'keyword2' => [
                        'value' => $qi,
                        'color' => '#173177'
                    ],
                    'keyword3' => [
                        'value' => $su,
                        'color' => '#173177'
                    ],
                    "remark" => [
                        "value" => "舱位信息：箱型：$tiji, 点击小程序了解更多信息。",
                        "color" => "#173177"
                    ]
                ]
            ];


            //防止中文被转码
            //转换为json
            $json = json_encode($data, JSON_UNESCAPED_UNICODE);


            //发送模板消息
            $msgid = Wechat::message()->sendTemplateMessage($json);

           
        }

       
    }
}
